var fs=require('fs');
var child_process = require('child_process'); 
var config=require('./config.json');
var protoPath='./protos';

var files=fs.readdirSync(protoPath);

function exportFiles(files){
    var contents=[];
    for(var file of files){
        contents.push({filename:file.substring(0,file.indexOf('.')),content:fs.readFileSync(protoPath+"/"+file).toString()});
    }
    var structs=require('./lib/protoParse')(contents);
    var object=require('./lib/fileFormat')(structs);
    //fs.writeFileSync(config.js_out+"/protos.json",JSON.stringify(object.config));
    fs.writeFileSync(config.js_out+"/protos.d.ts",object.ts);
    fs.writeFileSync(config.js_out+"/protos.js",object.js);
    //fs.writeFileSync(config.js_out+"/protosMap.json",JSON.stringify(object.clientMap));
    
    //fs.writeFileSync(config.java_out+"/protosMap.json",JSON.stringify(object.serverMap));
    for(var javaFile of object.javaFiles){
         fs.writeFileSync(config.java_out+"/"+javaFile.name+".java",javaFile.content);
    }
} 

exportFiles(files);


/////....
// function exportJavaHandler(file,method){
//     var protofile=protoPath+"/"+file;
//     var javeOut="--java_out="+config.java_out;
//     //['-H', ip, '-U', username, '-P', password, '-N', newpassword]
//     child_process.execFile('./lib/protobuf-2.5.0/protoc.exe',[javeOut,protofile],null,function (err, stdout, stderr) {
//         console.log(protofile);
//         method();
//     });
// }

// function exportJava(){
//     if(!files||!files.length){
//         console.log('导出完成!')
//         return;
//     }
//     var file=files.shift();
//     exportJavaHandler(file,exportJava);
// }
// exportJava();


